package com.itouch.bracelet.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.ImageView;

import com.itouch.bracelet.R;
import com.itouch.bracelet.diseaseinfo.Constants;
import com.itouch.util.CoFileUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

public class LoadingActivity extends BaseActivity {

	private ImageView img;
	private AnimationDrawable animationDrawable;
	private ImageLoader imageLoader = ImageLoader.getInstance();

	private String[] imgs = {
			"file://" + Environment.getExternalStorageDirectory() + "/"
					+ "img1.jpg",
			"file://" + Environment.getExternalStorageDirectory() + "/"
					+ "img2.jpg",
			"file://" + Environment.getExternalStorageDirectory() + "/"
					+ "img3.jpg",
			"file://" + Environment.getExternalStorageDirectory() + "/"
					+ "img4.jpg" };

	@Override
	protected int setContentViewById() {
		return R.layout.activity_loading;
	}

	@Override
	protected void initViews() {
		img = (ImageView) findViewById(R.id.img);
		img.setImageResource(R.drawable.activity_load_anim);
		animationDrawable = (AnimationDrawable) img.getDrawable();

//		try {
//			if (CoFileUtil.haveSDCard() && CoFileUtil.getSDFreeSize() > 5) {
//				
//				String saveDir = Environment.getExternalStorageDirectory()
//						+ Constants.PATH_PICTURE;
//				File dir = new File(saveDir);
//				if (!dir.exists()) {
//					dir.mkdirs(); // 创建文件夹
//				}
//				for (int i = 0; i < imgs.length; i++) {
//					String imgPathString = imgs[i];
//					File file = new File(imgPathString);
//					if (!file.exists()) {
//						boolean a = file.createNewFile();// 创建文件
//						if (a) {
//							Bitmap bitmap = imageLoader.loadImageSync("drawable://" + R.drawable.banner);
//							FileOutputStream out1 = new FileOutputStream(file);
//							bitmap.compress(CompressFormat.JPEG, 100, out1);
//						}
//					}
//
//				}
//
//			}
//		} catch (IOException e) {
//
//		}

	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		// animationDrawable.start();
		if (hasFocus) {
			new DelayTask().execute();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void initData() {

	}

	class DelayTask extends AsyncTask<Integer, String, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			animationDrawable.start();
		}

		@Override
		protected String doInBackground(Integer... arg0) {
			try {
				Thread.sleep(animationDrawable.getNumberOfFrames()
						* animationDrawable.getDuration(0));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			startActivity(new Intent(context, LoginActivity.class));
			LoadingActivity.this.finish();
		}

	}
}
